package com.kdtech.analyse.news;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.utils.HtmlCleaner;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class ZhuzhouwangNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		// http://zhuzhouwang.com/portal/xw/zzxw/bbrd/webinfo/2014/01/21/
		// 1390115347759905.htm
		boolean bRet = false;

		String[] regex = {
//			"http://www.zhuzhouwang.com/portal/.*[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]+.htm",
//			"http://zhuzhouwang.com/portal/.*[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.htm",
			"http://www.zhuzhouwang.com/[0-9\\/]+.shtml",
			"http://money.zhuzhouwang.com/news/[0-9\\/]+/storys_[0-9]+.html",
			"http://money.zhuzhouwang.com/news/[0-9\\/]+/story_[0-9]+.html",
			"http://digital.zhuzhouwang.com/[0-9]+/[0-9]+b.shtml",
			"http://auto.zhuzhouwang.com/news/storys_[0-9]+.html",
			
		};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet = true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		String html = urlMeta.getHtml();
		if (html == null) {
		}
		String title = "";
		String content = "";
		String author = "";
		Long date = 0l;
		int commentNum = 0;
		int clickNum = 0;
		NewsMeta meta = new NewsMeta();
		meta.setUrl(url);
		meta.setType(0);
		Document doc = Jsoup.parse(html);
		urlMeta = null;
		/*
		 * 解析新闻标题
		 */
		title = doc.select("h1").text();
		if (title.contains("来源")) {
			title = doc.select("title").text();
			title = StringUtils.substringBefore(title, "|");
		}
		/*
		 * 解析时间
		 */
		date = DateUtils.matchDate(doc.select("div.cs_title").text());
		if (date == null) {
			date = DateUtils.matchDate(doc.select("span").text());
		}
		/*
		 * 解析内容
		 */
		content = HtmlCleaner.getContentHtml(url, doc.select("div.article-content"));
		if (StringUtils.isBlank(content)) {
			content = HtmlCleaner.getContentHtml(url, doc.select("div.article"));
		}
		if (StringUtils.isBlank(content)) {
			content = HtmlCleaner.getContentHtml(url, doc.select("div.nry_zw"));
		}
		if (StringUtils.isBlank(content)) {
			content = HtmlCleaner.getContentHtml(url, doc.select("div.ina_news_pic_text"));
		}

		/*
		 * 作者
		 */
//		String authorStr = null;
		author = JSoupUtils.matchAuthor(doc, "来源:");

		if (StringUtils.isBlank(title)) {
		}
		meta.setTitle(title);
		meta.setContent(content);
		meta.setAuthor(author);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		return meta;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		String url = "http://auto.zhuzhouwang.com/news/storys_84355.html";
		UrlMeta urlMeta = CrawlHTML.responseToURL(url);
		ZhuzhouwangNewsAnalyse a = new ZhuzhouwangNewsAnalyse();
		System.out.println(a.isDetailPage(url));
			NewsMeta parserHtml = a.parserHtml(urlMeta);
			System.out.println(parserHtml);
		

	}

	
	public boolean isNeedUpdate() {
		return false;
	}
}
